package where2go.sctu.com.zhiyou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import where2go.sctu.com.zhiyou.MyApplication;
import where2go.sctu.com.zhiyou.R;
import where2go.sctu.com.zhiyou.entity.ResetPassword;
import where2go.sctu.com.zhiyou.entity.VerificationCode;

/**
 * ProjectName: zhiyou
 * PackageName: where2go.sctu.com.zhiyou.activity
 * Created by 张某人
 * CreatedTime:on 2018/5/25   16:44
 * Description: 重置邮箱密码
 */

public class ForgetPasswordActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText etForgetEmail;
    private EditText enterForgetVerify;
    private EditText etForgetPassword;
    private Button btnVerify;
    private Button btnSubmit;
    private ImageView btnBack;

    private VerificationCode verificationCode = new VerificationCode();
    private ResetPassword resetPassword = new ResetPassword();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);

        initView();
    }

    private void initView() {
        etForgetPassword = (EditText) findViewById(R.id.et_forget_password);
        etForgetEmail = (EditText) findViewById(R.id.et_forget);
        enterForgetVerify = (EditText) findViewById(R.id.et_enter_forget);

        btnBack = (ImageView) findViewById(R.id.forget_back);
        btnBack.setOnClickListener(this);

        btnSubmit = (Button) findViewById(R.id.btn_submit_forget);
        btnSubmit.setOnClickListener(this);

        btnVerify = (Button) findViewById(R.id.btn_forget);
        btnVerify.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_forget:
                String email = etForgetEmail.getText().toString().trim();
                String url = "http://47.104.80.155/youyiyou/users/getVerificationCode?email=";
                getVerificationCode(url + email);
                break;
            case R.id.btn_submit_forget:
                resetEmail();
                break;
            case R.id.forget_back:
                finish();
                break;
        }
    }

    /**
     * 重置邮箱密码
     */
    private void resetEmail() {
        String url = "http://47.104.80.155/youyiyou/users/resetPasswordByEmail?";
        StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                Type type = new TypeToken<ResetPassword>() {
                }.getType();
                //判断错误码
                resetPassword = gson.fromJson(response, type);
                int error = resetPassword.getError();
                if (error == 0) {
                    Toast.makeText(ForgetPasswordActivity.this, "重置密码成功", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(ForgetPasswordActivity.this, LoginActivity.class));
                } else if (error == 109 || error == 110 || error == 115 || error == 112 || error == 113) {
                    Toast.makeText(ForgetPasswordActivity.this, resetPassword.getMsg(),
                            Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(ForgetPasswordActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("email", etForgetEmail.getText().toString().trim());
                map.put("newpassword", etForgetPassword.getText().toString().trim());
                map.put("verificationCode", enterForgetVerify.getText().toString().trim());
                return map;
            }
        };
        request.setTag("resetPost");
        MyApplication.getHttpQueues().add(request);
    }

    /**
     * 发送验证码至邮箱
     *
     * @param url
     */
    private void getVerificationCode(String url) {
        StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                Type type = new TypeToken<VerificationCode>() {
                }.getType();
                //判断错误码
                verificationCode = gson.fromJson(response, type);
                int error = verificationCode.getError();
                if (error == 0) {
                    Toast.makeText(ForgetPasswordActivity.this, "验证码发送成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(ForgetPasswordActivity.this, "你还没输入邮箱哟", Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(ForgetPasswordActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
            }
        });
        request.setTag("getVerificationCode");
        MyApplication.getHttpQueues().add(request);
    }
}
